home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
242_01
/
a63util.c
< prev
next >
Wrap
Text File
|
1989-01-11
|
16KB
|
564 lines
/*
HEADER: CUG242;
TITLE: 63701 Cross-Assembler (Portable);
FILENAME: A63UTIL.C;
VERSION: 0.1;
DATE: 08/27/1988;
SEE-ALSO: A63.H;
AUTHORS: William C. Colley III;
*/
/*
63701 Cross-Assembler in Portable C
Copyright (c) 1985,1987 William C. Colley, III
Revision History:
Ver Date Description
0.0 AUG 1987 Derived from version 3.4 of my portable 6800/6801
cross-assembler. WCC3.
0.1 AUG 1988 Fixed a bug in the command line parser that puts it
into a VERY long loop if the user types a command line
like "A63 FILE.ASM -L". WCC3 per Alex Cameron.
This module contains the following utility packages:
1) symbol table building and searching
2) opcode and operator table searching
3) listing file output
4) hex file output
5) error flagging
*/
/* Get global goodies: */
#include "a63.h"
/* Make sure that MSDOS compilers using the large memory model know */
/* that calloc() returns pointer to char as an MSDOS far pointer is */
/* NOT compatible with the int type as is usually the case. */
char *calloc();
/* Get access to global mailboxes defined in A63.C: */
extern char errcode, line[], title[];
extern int eject, listhex;
extern unsigned address, bytes, errors, listleft, obj[], pagelen;
/* The symbol table is a binary tree of variable-length blocks drawn */
/* from the heap with the calloc() function. The root pointer lives */
/* here: */
static SYMBOL *sroot = NULL;
/* Add new symbol to symbol table. Returns pointer to symbol even if */
/* the symbol already exists. If there's not enough memory to store */
/* the new symbol, a fatal error occurs. */
SYMBOL *new_symbol(nam)
char *nam;
{
SCRATCH int i;
SCRATCH SYMBOL **p, *q;
void fatal_error();
for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
p = i < 0 ? &(q -> left) : &(q -> right);
if (!q) {
if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
fatal_error(SYMBOLS);
strcpy(q -> sname,nam);
}
return q;
}
/* Look up symbol in symbol table. Returns pointer to symbol or NULL */
/* if symbol not found. */
SYMBOL *find_symbol(nam)
char *nam;
{
SCRATCH int i;
SCRATCH SYMBOL *p;
for (p = sroot; p && (i = strcmp(nam,p -> sname));
p = i < 0 ? p -> left : p -> right);
return p;
}
/* Opcode table search routine. This routine pats down the opcode */
/* table for a given opcode and returns either a pointer to it or */
/* NULL if the opcode doesn't exist. */
OPCODE *find_code(nam)
char *nam;
{
OPCODE *bsearch();
static OPCODE opctbl[] = {
{ NULL, 0x1b, "ABA" },
{ NULL, 0x3a, "ABX" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x89, "ADC" },
{ DIROK + INDEX + IMM8, 0x89, "ADCA" },
{ DIROK + INDEX + IMM8, 0xc9, "ADCB" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x8b, "ADD" },
{ DIROK + INDEX + IMM8, 0x8b, "ADDA" },
{ DIROK + INDEX + IMM8, 0xcb, "ADDB" },
{ DIROK + INDEX + IMM16, 0xc3, "ADDD" },
{ TWO_OP + INDEX, 0x71, "AIM" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x84, "AND" },
{ DIROK + INDEX + IMM8, 0x84, "ANDA" },
{ DIROK + INDEX + IMM8, 0xc4, "ANDB" },
{ INDEX + REGOPT, 0x48, "ASL" },
{ NULL, 0x48, "ASLA" },
{ NULL, 0x58, "ASLB" },
{ NULL, 0x05, "ASLD" },
{ INDEX + REGOPT, 0x47, "ASR" },
{ NULL, 0x47, "ASRA" },
{ NULL, 0x57, "ASRB" },
{ REL, 0x24, "BCC" },
{ TWO_OP + BIT_OP + INDEX, 0x71, "BCLR" },
{ REL, 0x25, "BCS" },
{ REL, 0x27, "BEQ" },
{ REL, 0x2c, "BGE" },
{ REL, 0x2e, "BGT" },
{ REL, 0x22, "BHI" },
{ REL, 0x24, "BHS" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x85, "BIT" },
{ DIROK + INDEX + IMM8, 0x85, "BITA" },
{ DIROK + INDEX + IMM8, 0xc5, "BITB" },
{ REL, 0x2f, "BLE" },
{ REL, 0x25, "BLO" },
{ REL, 0x23, "BLS" },
{ REL, 0x2d, "BLT" },
{ REL, 0x2b, "BMI" },
{ REL, 0x26, "BNE" },
{ REL, 0x2A, "BPL" },
{ REL, 0x20, "BRA" },
{ REL, 0x21, "BRN" },
{ TWO_OP + BIT_OP + INDEX, 0x72, "BSET" },
{ REL, 0x8d, "BSR" },
{ TWO_OP + BIT_OP + INDEX, 0x75, "BTGL" },
{ TWO_OP + BIT_OP + INDEX, 0x7b, "BTST" },
{ REL, 0x28, "BVC" },
{ REL, 0x29, "BVS" },
{ NULL, 0x11, "CBA" },
{ NULL, 0x0c, "CLC" },
{ NULL, 0x0e, "CLI" },
{ INDEX + REGOPT, 0x4f, "CLR" },
{ NULL, 0x4f, "CLRA" },
{ NULL, 0x5f, "CLRB" },
{ NULL, 0x0a, "CLV" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x81, "CMP" },
{ DIROK + INDEX + IMM8, 0x81, "CMPA" },
{ DIROK + INDEX + IMM8, 0xc1, "CMPB" },
{ INDEX + REGOPT, 0x43, "COM" },
{ NULL, 0x43, "COMA" },
{ NULL, 0x53, "COMB" },
{ DIROK + INDEX + IMM16, 0x8c, "CPX" },
{ NULL, 0x19, "DAA" },
{ INDEX + REGOPT, 0x4a, "DEC" },
{ NULL, 0x4a, "DECA" },
{ NULL, 0x5a, "DECB" },
{ NULL, 0x34, "DES" },
{ NULL, 0x09, "DEX" },
{ TWO_OP + INDEX, 0x75, "EIM" },
{ PSEUDO + ISIF, ELSE, "ELSE" },
{ PSEUDO, END, "END" },
{ PSEUDO + ISIF, ENDI, "ENDI" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x88, "EOR" },
{ DIROK + INDEX + IMM8, 0x88, "EORA" },
{ DIROK + INDEX + IMM8, 0xc8, "EORB" },
{ PSEUDO, EQU, "EQU" },
{ PSEUDO, FCB, "FCB" },
{ PSEUDO, FCC, "FCC" },
{ PSEUDO, FDB, "FDB" },
{ PSEUDO + ISIF, IF, "IF" },
{ INDEX + REGOPT, 0x4c, "INC" },
{ NULL, 0x4c, "INCA" },
{ NULL, 0x5c, "INCB" },
{ PSEUDO, INCL, "INCL" },
{ NULL, 0x31, "INS" },
{ NULL, 0x08, "INX" },
{ INDEX, 0x4e, "JMP" },
{ DIROK + INDEX, 0x8d, "JSR" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x86, "LDA" },
{ DIROK + INDEX + IMM8, 0x86, "LDAA" },
{ DIROK + INDEX + IMM8, 0xc6, "LDAB" },
{ DIROK + INDEX + IMM16, 0xcc, "LDD" },
{ DIROK + INDEX + IMM16, 0x8e, "LDS" },
{ DIROK + INDEX + IMM16, 0xce, "LDX" },
{ INDEX + REGOPT, 0x48, "LSL" },
{ NULL, 0x48, "LSLA" },
{ NULL, 0x58, "LSLB" },
{ NULL, 0x05, "LSLD" },
{ INDEX + REGOPT, 0x44, "LSR" },
{ NULL, 0x44, "LSRA" },
{ NULL, 0x54, "LSRB" },
{ NULL, 0x04, "LSRD" },
{ NULL, 0x3d, "MUL" },
{ INDEX + REGOPT, 0x40, "NEG" },
{ NULL, 0x40, "NEGA" },
{ NULL, 0x50, "NEGB" },
{ NULL, 0x01, "NOP" },
{ TWO_OP + INDEX, 0x72, "OIM" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x8a, "ORA" },
{ DIROK + INDEX + IMM8, 0x8a, "ORAA" },
{ DIROK + INDEX + IMM8, 0xca, "ORAB" },
{ PSEUDO, ORG, "ORG" },
{ PSEUDO, PAGE, "PAGE" },
{ REGREQ, 0x36, "PSH" },
{ NULL, 0x36, "PSHA" },
{ NULL, 0x37, "PSHB" },
{ NULL, 0x3c, "PSHX" },
{ REGREQ, 0x32, "PUL" },
{ NULL, 0x32, "PULA" },
{ NULL, 0x33, "PULB" },
{ NULL, 0x38, "PULX" },
{ PSEUDO, RMB, "RMB" },
{ INDEX + REGOPT, 0x49, "ROL" },
{ NULL, 0x49, "ROLA" },
{ NULL, 0x59, "ROLB" },
{ INDEX + REGOPT, 0x46, "ROR" },
{ NULL, 0x46, "RORA" },
{ NULL, 0x56, "RORB" },
{ NULL, 0x3b, "RTI" },
{ NULL, 0x39, "RTS" },
{ NULL, 0x10, "SBA" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x82, "SBC" },
{ DIROK + INDEX + IMM8, 0x82, "SBCA" },
{ DIROK + INDEX + IMM8, 0xc2, "SBCB" },
{ NULL, 0x0d, "SEC" },
{ NULL, 0x0f, "SEI" },
{ PSEUDO, SET, "SET" },
{ NULL, 0x0b, "SEV" },
{ NULL, 0x1a, "SLP" },
{ DIROK + INDEX + REGREQ, 0x87, "STA" },
{ DIROK + INDEX, 0x87, "STAA" },
{ DIROK + INDEX, 0xc7, "STAB" },
{ DIROK + INDEX, 0xcd, "STD" },
{ DIROK + INDEX, 0x8f, "STS" },
{ DIROK + INDEX, 0xcf, "STX" },
{ DIROK + INDEX + IMM8 + REGREQ, 0x80, "SUB" },
{ DIROK + INDEX + IMM8, 0x80, "SUBA" },
{ DIROK + INDEX + IMM8, 0xc0, "SUBB" },
{ DIROK + INDEX + IMM16, 0x83, "SUBD" },
{ NULL, 0x3f, "SWI" },
{ NULL, 0x16, "TAB" },
{ NULL, 0x06, "TAP" },
{ NULL, 0x17, "TBA" },
{ TWO_OP + INDEX, 0x7b, "TIM" },
{ PSEUDO, TITL, "TITL" },
{ NULL, 0x07, "TPA" },
{ INDEX + REGOPT, 0x4d, "TST" },
{ NULL, 0x4d, "TSTA" },
{ NULL, 0x5d, "TSTB" },
{ NULL, 0x30, "TSX" },
{ NULL,